/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formalStuTableDialog.java
 *
 * Created on 2011-5-9, 9:42:20
 */
package junchangmis.student;

import java.sql.SQLException;
import junchangmis.MainFrame;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.write.WriteException;
import publicmethod.*;
import junchangmis.codecSetting.*;
import javax.swing.JOptionPane;
import java.awt.Point;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author developers
 */
public class formalStuTableDialog extends javax.swing.JDialog {

    MainFrame parent;
    tipDialog td = new tipDialog(this, false);

    /** Creates new form formalStuTableDialog */
    public formalStuTableDialog(java.awt.Frame parent, boolean modal, MainFrame pparent) {
        super(parent, modal);
        this.parent = pparent;
        initComponents();
        this.jButtonConfirm.setEnabled(false);
        this.setLocationRelativeTo(null);
        tipDialog();
        this.setTitle("班级选择");
    }

    void tipDialog() {
        Point p = new Point(this.getX() + this.getWidth(), this.getY());
        td.setLocation(p);
        td.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelClassCode = new javax.swing.JLabel();
        jTextFieldClassCode = new javax.swing.JTextField();
        jLabelCodeShow = new javax.swing.JLabel();
        jCheckBoxPrint = new javax.swing.JCheckBox();
        jButtonConfirm = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabelClassCode.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabelClassCode.setText("输入专业班级编码");
        jPanel1.add(jLabelClassCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jTextFieldClassCode.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jTextFieldClassCode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldClassCodeActionPerformed(evt);
            }
        });
        jTextFieldClassCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldClassCodeKeyReleased(evt);
            }
        });
        jPanel1.add(jTextFieldClassCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 10, 180, -1));

        jLabelCodeShow.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabelCodeShow.setText("——");
        jPanel1.add(jLabelCodeShow, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 60, 200, 50));

        jCheckBoxPrint.setFont(new java.awt.Font("华文细黑", 0, 24));
        jCheckBoxPrint.setText("是否打印");
        jCheckBoxPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxPrintActionPerformed(evt);
            }
        });
        jPanel1.add(jCheckBoxPrint, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, -1, -1));

        jButtonConfirm.setFont(new java.awt.Font("华文细黑", 0, 24));
        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonConfirm, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 160, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldClassCodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldClassCodeActionPerformed
        // TODO add your handling code here:
        String classCode = this.jTextFieldClassCode.getText();
        if (classCode == null || classCode.length() != 7) {
            JOptionPane.showConfirmDialog(null, "专业班级的编码格式错误！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            this.jLabelCodeShow.setText("");
            this.jTextFieldClassCode.setSelectionStart(0);
            this.jButtonConfirm.setEnabled(false);
        } else {
            String codeTriple = codecMethodNew.getMeaning(this.jTextFieldClassCode.getText());//获得总编码的翻译结果
            if (codeTriple == null) {
                JOptionPane.showConfirmDialog(null, "不存在该专业班级编码！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
                this.jLabelCodeShow.setText("");
                this.jTextFieldClassCode.setSelectionStart(0);
                this.jButtonConfirm.setEnabled(false);
                return;
            }
            this.jLabelCodeShow.setText("：" + codeTriple);
            this.jButtonConfirm.setEnabled(true);//只有编码格式正确后，确定按钮才可以用
        }
}//GEN-LAST:event_jTextFieldClassCodeActionPerformed

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        String codeTriple = codecMethodNew.getMeaning(this.jTextFieldClassCode.getText());//获得总编码的翻译结果
        if (codeTriple == null) {
            JOptionPane.showConfirmDialog(null, "不存在该专业班级编码！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            this.jLabelCodeShow.setText("");
            this.jTextFieldClassCode.setSelectionStart(0);
            this.jButtonConfirm.setEnabled(false);
            return;
        } else {
            try {
                // TODO add your handling code here:
                // TODO add your handling code here:
                formalStuTableExcel work = new formalStuTableExcel(this.jTextFieldClassCode.getText());
                String filename = "交费登记表\\";
                filename = filename + this.jTextFieldClassCode.getText() + "班交费登记表.xls";
                work.setOutputFile(filename);
                work.write();
                System.out.println("Please check the result file " + filename);
                JOptionPane.showConfirmDialog(null, "生成成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
                if (this.jCheckBoxPrint.isSelected() == true) {
                    PrintTest.print(PrintTest.getPath() + "\\" + filename);
                } else {
                    PrintTest.showTable(PrintTest.getPath() + "\\" + filename);
                }
                //this.parent.setVisible(true);//显示父窗口
                dispose();//关闭自己

            } catch (WriteException ex) {
                Logger.getLogger(newComerTableDialog.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(newComerTableDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    private void jCheckBoxPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxPrintActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxPrintActionPerformed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
        tipDialog();
    }//GEN-LAST:event_formMouseReleased
    boolean isBlank(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }
    private void jTextFieldClassCodeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldClassCodeKeyReleased
        // TODO add your handling code here:
        if (isBlank(this.jTextFieldClassCode.getText())) {
            td.getItem(null);
        } else {
            try {
                // TODO add your handling code here:
                Vector<String> codes = new Vector<String>();
                DbOperation db = new DbOperation();
                db.DbConnect();
                String sql = "select * from codeNew";
                ResultSet rs = db.DBSqlQuery(sql);
                while (rs.next()) {
                    String code = rs.getString(1);
                    String meaning = rs.getString(2);
                    if (code.startsWith(this.jTextFieldClassCode.getText())) {
                        codes.add(code + "    " + meaning);
                    }
                }
                db.DbClose();
                td.getItem(codes);
            } catch (SQLException ex) {
                Logger.getLogger(formalStuTableDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jTextFieldClassCodeKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*   java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
        formalStuTableDialog dialog = new formalStuTableDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
        }
        });
        dialog.setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JCheckBox jCheckBoxPrint;
    private javax.swing.JLabel jLabelClassCode;
    private javax.swing.JLabel jLabelCodeShow;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldClassCode;
    // End of variables declaration//GEN-END:variables
}
